package com.sanji.sjmallmodule.phoneshell;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.sanji.sjmallapp.R;
import com.sanji.sjmallutils.ImageUtil;
import com.sanji.sjmallutils.Tools;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 手机壳
 * Created by ZSF on 17/9/7.
 */

public class PhoneShellAdapter extends BaseAdapter {

    private Context mCtx;
    private List<ShellGoodsBean> goodsList = new ArrayList<>();

    public PhoneShellAdapter(Context context,List<ShellGoodsBean> list){
        this.mCtx = context;
        this.goodsList = list;
    }

    public void notify(List<ShellGoodsBean> list){
        this.goodsList = list;
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return goodsList.size();
    }

    @Override
    public ShellGoodsBean getItem(int i) {
        return goodsList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ShellHolder holder;
        if (view == null){
            view = LayoutInflater.from(mCtx).inflate(R.layout.adapter_phone_shell,null);
            holder=new ShellHolder(view);
            int imageWidth = ImageUtil.setImageWidth(mCtx, 4);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(imageWidth,imageWidth);
            holder.headImg.setLayoutParams(params);

            view.setTag(holder);

        }else{
            holder = (ShellHolder) view.getTag();
        }

        ShellGoodsBean goods = getItem(i);
        if (goods!=null){
            holder.titleTv.setText(goods.getTitle() +" "+ goods.getCategoryName());
            Glide.with(mCtx).load(goods.getImageUrl())
                    //使用centerCrop是利用图片图填充ImageView设置的大小，如果ImageView的Height是match_parent则图片就会被拉伸填充
                    .centerCrop()
                    //当加载网络图片时，由于加载过程中图片未能及时显示，此时可能需要设置等待时的图片，通过placeHolder()方法
                    .placeholder(R.mipmap.default_3j_img)
                    //当加载图片失败时，通过error(Drawable drawable)方法设置加载失败后的图片显示
                    .error(R.mipmap.default_3j_img)
                    .crossFade()
                    //DiskCacheStrategy.SOURCE：缓存原始数据，DiskCacheStrategy.RESULT：缓存变换(如缩放、裁剪等)后的资源数据，
                    // DiskCacheStrategy.NONE：什么都不缓存，DiskCacheStrategy.ALL：缓存SOURC和RESULT。
                    // 默认采用DiskCacheStrategy.RESULT策略，对于download only操作要使用DiskCacheStrategy.SOURCE。
//                .diskCacheStrategy(DiskCacheStrategy.RESULT)
                    .into(holder.headImg);
            holder.typeTv.setText(goods.getTechnology());
            holder.priceTv.setText("￥"+ Tools.divide(goods.getMembersPrice()));
        }


        return view;
    }

    class ShellHolder{
        @BindView(R.id.adapter_phone_shell_head_img)ImageView headImg;
        @BindView(R.id.adapter_phone_shell_title_tv)TextView titleTv;
        @BindView(R.id.adapter_phone_shell_type_tv)TextView typeTv;
        @BindView(R.id.adapter_phone_shell_price_tv)TextView priceTv;

        public ShellHolder(View view){
            ButterKnife.bind(this,view);
        }
    }
}
